package com.xc.copy;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class TestCopyBean {
    public static void main(String[] args) {
        Cla cla = new Cla("九年级2班");
        StuInfo info = new StuInfo();
        info.setCla(cla);
        hutoolCopyClass(info);
        hutoolCopyEntry(info);
        beansCopyEntry(info);
        BeansCopyClass(info);
    }

    // hutool工具包copyProperties.class
    public static void hutoolCopyClass(StuInfo info) {// 用这个接收了，但是info是灰色
        Stu stu = new Stu("张三");
        info = BeanUtil.copyProperties(stu, StuInfo.class);
        System.out.println("hutool工具包copyProperties.class：：：" + info);
    }

    // hutool工具包copyPropertiesEntry
    public static void hutoolCopyEntry(StuInfo info) {
        Stu stu = new Stu("张三");
        BeanUtil.copyProperties(stu, info);
        System.out.println("hutool工具包copyPropertiesEntry ：：：" + info);
    }

    // Beans包的copyPropertiesEntry
    public static void beansCopyEntry(StuInfo info) {
        Stu stu = new Stu("张三");
        BeanUtils.copyProperties(stu, info);
        System.out.println("Beans包的copyPropertiesEntry  ：：：" + info);
    }

    // Beans包的copyProperties.Class
    public static void BeansCopyClass(StuInfo info) {
        Stu stu = new Stu("张三");
        BeanUtils.copyProperties(stu, StuInfo.class);
        System.out.println("Beans包的copyProperties.class  ：：：" + info);
    }
}

class TestCopyList {
    /**
     * BeanUtil.copyToList()
     * stream——>map(new)
     * list.addAll(list)
     * new ArrayList<>(list)
     * Collections.copy
     * CollectionUtils.addAll()
     * CollUtil.newArrayList()
     *
     * @param args
     */
    public static void main(String[] args) {
        List<Cla> claList = new ArrayList<>();
        Collections.addAll(claList, new Cla("1"), new Cla("2"), new Cla("3"));
        // 依旧会删除源数据
        beanUtilCopyToList(claList);
        // class com.xc.copy.Cla cannot be cast to class com.xc.copy.Stu
        // 数据类型要求要一致
        listAddAll(claList);
        // 必须目标list长度 ≥ 原list长度，否则报错。设置长度一直失败，不好用
        collectionsCopy(claList);
        // class com.xc.copy.Cla cannot be cast to class com.xc.copy.Stu
        // 居然还把源数据输出出来了
        // import org.apache.commons.collections4.CollectionUtils;
        collectionUtilsAddAll(claList);
        // class com.xc.copy.Cla cannot be cast to class com.xc.copy.Stu
        // 源数据也没输出
        collUtilNewArrayList(claList);
        // class com.xc.copy.Cla cannot be cast to class com.xc.copy.Stu
        newArrayList(claList);
        // 删除了源数据
        streamMapNew(claList);
    }

    // BeanUtil.copyToList()
    public static void beanUtilCopyToList(List claList) {
        List<Stu> stuList = new ArrayList<>();
        stuList.add(new Stu("stu源数据"));
        // 后面的class类型为集合内实体类的类型，并非集合的字节码
        // 依旧会删除源数据
        stuList = BeanUtil.copyToList(claList, Stu.class);
        stuList.forEach(System.out::print);
    }

    // list.addAll
    public static void listAddAll(List claList) {
        List<Stu> stuList = new ArrayList<>();
        stuList.add(new Stu("stu源数据"));
        // class com.xc.copy.Cla cannot be cast to class com.xc.copy.Stu
        // 要求原集合类型跟目标集合类型一致
        stuList.addAll(claList);
        stuList.forEach(System.out::print);
    }

    // Collections.copy
    public static void collectionsCopy(List claList) {
        List<Stu> stuList = new ArrayList<>();
        stuList.add(new Stu("stu源数据"));
        // 必须目标list长度 ≥ 原list长度，否则报错。设置长度一直失败，不好用
        Collections.copy(stuList, claList);
        stuList.forEach(System.out::print);
    }

    // CollectionUtils.addAll
    public static void collectionUtilsAddAll(List claList) {
        List<Stu> stuList = new ArrayList<>();
        stuList.add(new Stu("stu源数据"));
        // class com.xc.copy.Cla cannot be cast to class com.xc.copy.Stu
        // 居然还把源数据输出出来了
        // import org.apache.commons.collections4.CollectionUtils;
        CollectionUtils.addAll(stuList, claList);
        stuList.forEach(System.out::print);
    }

    // CollUtil.newArrayList()
    public static void collUtilNewArrayList(List claList) {
        List<Stu> stuList = new ArrayList<>();
        stuList.add(new Stu("stu源数据"));
        // class com.xc.copy.Cla cannot be cast to class com.xc.copy.Stu
        // 源数据也没输出
        stuList = CollUtil.newArrayList(claList);
        stuList.forEach(System.out::print);
    }

    // newArrayList()
    public static void newArrayList(List claList) {
        // class com.xc.copy.Cla cannot be cast to class com.xc.copy.Stu
        List<Stu> stuList = new ArrayList<>(claList);
        stuList.add(new Stu("stu源数据"));
        stuList.forEach(System.out::print);
    }

    // stream.map(new)
    public static void streamMapNew(List claList) {
        List<Stu> stuList = new ArrayList<>();
        stuList.add(new Stu("stu源数据"));
        // 删除了源数据
        stuList = claList.stream().map(cl -> {
            Stu stu = new Stu();
            BeanUtils.copyProperties(cl, stu);
            return stu;
        }).toList();
        stuList.forEach(System.out::print);
    }
}

@Data
@NoArgsConstructor
@AllArgsConstructor
class StuInfo {
    private Cla cla;
    private String name;
}

@Data
@NoArgsConstructor
@AllArgsConstructor
class Cla {
    private String name;
}

@Data
@NoArgsConstructor
@AllArgsConstructor
class Stu {
    private String name;
}